import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
function fn(msg,next){
  let routeEnter = store.state.user.menus_url
  if(routeEnter.includes(msg)){
    next()
  }else{
    next("/")
  }
}
Vue.use(VueRouter)
export const secondRouter=[
  {
    path:"banner",
    meta:{
      title:"轮播"
    },
    beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/banner")) {
        next()
      }else{
        next("/")
      }
    },
    component:()=>import("../page/banner/banner.vue")
  },
  {
    path:"/",
    meta:{
      title:"home"
    },
    component:()=>import("../page/home/home.vue")
  },
  {
    path:"goods",
    meta:{
      title:"商品管理"
    },
    component:()=>import("../page/goods/goods.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/goods")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"manage",
    meta:{
      title:"管理员管理"
    },
    component:()=>import("../page/manage/manage.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/manage")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"member",
    meta:{
      title:"会员管理"
    },
    component:()=>import("../page/member/member.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/member")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"role",
    meta:{
      title:"角色管理"
    },
    component:()=>import("../page/role/role.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/role")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"seckill",
    meta:{
      title:"秒杀活动"
    },
    component:()=>import("../page/seckill/seckill.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/seckill")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"menu",
    meta:{
      title:"菜单管理"
    },
    component:()=>import("../page/menu/menu.vue"),
    beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/menu")) {
        next()
      }else{
        next("/")
      }
    },
    
  },

  {
    path:"cate",
    meta:{
      title:"商品分类"
    },
    component:()=>import("../page/cate/cate.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/cate")) {
        next()
      }else{
        next("/")
      }
    },
  },
  {
    path:"specs",
    meta:{
      title:"商品规格"
    },
    component:()=>import("../page/specs/specs.vue")
    ,beforeEnter: (to, from, next) => {
      let rounterenter=store.state.user.menus_url
      // 返回true/false
      if (rounterenter.includes("/specs")) {
        next()
      }else{
        next("/")
      }
    },
  },
  
]

const routes = [
  {
    path:"/login",
    component: ()=> import("../page/login/login.vue")
  },{
    path:"/",
    component: ()=> import("../page/index/index.vue"),
    children:[
      {
        path:"banner",
        meta:{
          title:"轮播"
        },
        component:()=>import("../page/banner/banner.vue")
        ,beforeEnter: (to, from, next) => {
          let rounterenter=store.state.user.menus_url
          // 返回true/false
          if (rounterenter.includes("/banner")) {
            next()
          }else{
            next("/")
          }
        },
      },
      {
        path:"",
        meta:{
          title:"home"
        },
        component:()=>import("../page/home/home.vue")
      },
      
        ...secondRouter
      
    ]
  }
  
]

const router = new VueRouter({
  routes

})
// router.beforeEach((to,from,next)=>{
//   //登陆不拦截
//   if (to.path== "/login") {
//     next()
//     return
//   }
//   if (store.state.user.token) {
//       next()
//       return
//   }
//   next("/login")
// })

export default router
